package com.hdrx.fundhelper.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;




/**
 * @author: houdx
 * @description:
 * @date: 2023/8/11 15:34
 */
@Component
@ConfigurationProperties(prefix = "mq")
public class RabbitSender {
    @Value("${mq.directexchange}")
    private String directExchange;


    // 自定义的模板，所有的消息都会转换成JSON发送
    @Autowired
    AmqpTemplate amqpTemplate;


    public void send(Map<String, Object> map, String directRoutingKey) {
        // 发送JSON字符串
        ObjectMapper mapper = new ObjectMapper();
        String sycMsg = null;
        try {
            sycMsg = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(sycMsg);
        amqpTemplate.convertAndSend(directExchange, directRoutingKey, sycMsg);
    }


}
